﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using MvcApplication2.Models;

namespace MvcApplication2.Controllers
{ 
    public class YkienController : Controller
    {
        private MvcApplication2Context db = new MvcApplication2Context();

        //
        // GET: /Ykien/

        public ViewResult Index()
        {
            var ykiens = db.Ykiens.Include(y => y.Khachhang);
            return View(ykiens.ToList());
        }

        //
        // GET: /Ykien/Details/5

        public ViewResult Details(int id)
        {
            Ykien ykien = db.Ykiens.Find(id);
            return View(ykien);
        }

        //
        // GET: /Ykien/Create

        public ActionResult Create()
        {
            ViewBag.KhachhangID = new SelectList(db.Khachhangs, "KhachhangID", "Tenkhachhang");
            return View();
        } 

        //
        // POST: /Ykien/Create

        [HttpPost]
        public ActionResult Create(Ykien ykien)
        {
            if (ModelState.IsValid)
            {
                db.Ykiens.Add(ykien);
                db.SaveChanges();
                return RedirectToAction("Index");  
            }

            ViewBag.KhachhangID = new SelectList(db.Khachhangs, "KhachhangID", "Tenkhachhang", ykien.KhachhangID);
            return View(ykien);
        }
        
        //
        // GET: /Ykien/Edit/5
 
        public ActionResult Edit(int id)
        {
            Ykien ykien = db.Ykiens.Find(id);
            ViewBag.KhachhangID = new SelectList(db.Khachhangs, "KhachhangID", "Tenkhachhang", ykien.KhachhangID);
            return View(ykien);
        }

        //
        // POST: /Ykien/Edit/5

        [HttpPost]
        public ActionResult Edit(Ykien ykien)
        {
            if (ModelState.IsValid)
            {
                db.Entry(ykien).State = EntityState.Modified;
                db.SaveChanges();
                return RedirectToAction("Index");
            }
            ViewBag.KhachhangID = new SelectList(db.Khachhangs, "KhachhangID", "Tenkhachhang", ykien.KhachhangID);
            return View(ykien);
        }

        //
        // GET: /Ykien/Delete/5
 
        public ActionResult Delete(int id)
        {
            Ykien ykien = db.Ykiens.Find(id);
            return View(ykien);
        }

        //
        // POST: /Ykien/Delete/5

        [HttpPost, ActionName("Delete")]
        public ActionResult DeleteConfirmed(int id)
        {            
            Ykien ykien = db.Ykiens.Find(id);
            db.Ykiens.Remove(ykien);
            db.SaveChanges();
            return RedirectToAction("Index");
        }

        protected override void Dispose(bool disposing)
        {
            db.Dispose();
            base.Dispose(disposing);
        }
    }
}